package com.jushu.storbox.database.metadata;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.jushu.storbox.database.helper.SdCardDBOpenHelper;
import com.jushu.storbox.entities.NodeFiles;

public class RemoteNodeFilesOperation {

	private SQLiteOpenHelper dbOpenHelper;
	
	/**
	 * @param context
	 * @param databaseName
	 * @param type
	 */
	public RemoteNodeFilesOperation(Context context, File dbFile) {
		this.dbOpenHelper = new SdCardDBOpenHelper(context, dbFile, null);
	}

	public long add(NodeFiles filesInode) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		ContentValues values = filesInode.toContentValues();
		return db.insert(NodeFiles.TABLE_NANME_4_CHANGE_METADATA, null, values);
	}

	public int delte(String whereCase, String[] whereArgs) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		return db.delete(NodeFiles.TABLE_NANME_4_CHANGE_METADATA, whereCase, whereArgs);
	}

	public int update(NodeFiles filesInode, String whereCase, String[] whereArgs) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		return db.update( NodeFiles.TABLE_NANME_4_CHANGE_METADATA,
				filesInode.toContentValues(),
				whereCase,
				whereArgs);
	}

	public List<NodeFiles> listAll() {
		List<NodeFiles> list = null;
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.query(NodeFiles.TABLE_NANME_4_CHANGE_METADATA, null, null, null, null, null, null);
		if (null != cursor) {
			list = new ArrayList<NodeFiles>();
			while (cursor.moveToNext()) {
				NodeFiles filesINode = new NodeFiles();
				filesINode.fromCursor(cursor);
				list.add(filesINode);
			}
			cursor.close();
		}
		return list;
	}
	
	public List<NodeFiles> listFolders() {
		List<NodeFiles> list = null;
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		String select = NodeFiles.Fields.TYPE + "=?";
		String[] selectArg = new String[]{"0"};
		Cursor cursor = db.query(NodeFiles.TABLE_NANME_4_CHANGE_METADATA, null, select, selectArg, null, null, null);
		if (null != cursor) {
			list = new ArrayList<NodeFiles>();
			while (cursor.moveToNext()) {
				NodeFiles filesINode = new NodeFiles();
				filesINode.fromCursor(cursor);
				list.add(filesINode);
			}
			cursor.close();
		}
		return list;
	}
	
	public List<NodeFiles> listFiles() {
		List<NodeFiles> list = null;
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		String select = NodeFiles.Fields.TYPE + "=?";
		String[] selectArg = new String[]{"1"};
		Cursor cursor = db.query(NodeFiles.TABLE_NANME_4_CHANGE_METADATA, null, select, selectArg , null, null, null);
		if (null != cursor) {
			list = new ArrayList<NodeFiles>();
			while (cursor.moveToNext()) {
				NodeFiles filesINode = new NodeFiles();
				filesINode.fromCursor(cursor);
				list.add(filesINode);
			}
			cursor.close();
		}
		return list;
	}
	
	public HashMap<String, NodeFiles> mapFolders() {
		HashMap<String,NodeFiles> map = null;
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		String select = NodeFiles.Fields.TYPE + "=?";
		String[] selectArg = new String[]{"0"};
		Cursor cursor = db.query(NodeFiles.TABLE_NANME_4_CHANGE_METADATA, null, select, selectArg, null, null, null);
		if (null != cursor) {
			map = new HashMap<String,NodeFiles>();
			while (cursor.moveToNext()) {
				NodeFiles filesINode = new NodeFiles();
				filesINode.fromCursor(cursor);
				map.put(filesINode.getStringId(), filesINode);
			}
			cursor.close();
		}
		return map;
	}
	
	public HashMap<String, NodeFiles> mapFiles() {
		HashMap<String,NodeFiles> map = null;
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		String select = NodeFiles.Fields.TYPE + "=?";
		String[] selectArg = new String[]{"1"};
		Cursor cursor = db.query(NodeFiles.TABLE_NANME_4_CHANGE_METADATA, null, select, selectArg , null, null, null);
		if (null != cursor) {
			map = new HashMap<String,NodeFiles>();
			while (cursor.moveToNext()) {
				NodeFiles filesINode = new NodeFiles();
				filesINode.fromCursor(cursor);
				map.put(filesINode.getStringId(), filesINode);
			}
			cursor.close();
		}
		return map;
	}

	public NodeFiles findById(Integer id) {
		return findById(String.valueOf(id));
	}
	
	public NodeFiles findById(String id) {
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		NodeFiles filesINode = null;
		Cursor cursor = db.query(NodeFiles.TABLE_NANME_4_CHANGE_METADATA, null,
				NodeFiles.Fields.ID + "=?",
				new String[] { id},
				null, null, null);
		if (null != cursor) {
			filesINode = new NodeFiles();
			filesINode.fromCursor(cursor);
			cursor.close();
		}
		return filesINode;
	}
}
